package com.hliushi.learn.config;

import com.hliushi.learn.interceptor.MyInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * @ClassName MyInterceptorConfig
 * @Description 配置我们自定义的拦截器
 * @Author liushi
 * @Date 2021/7/8 19:59
 * @Version V1.0
 **/
@Configuration
public class MyInterceptorConfig extends WebMvcConfigurationSupport {

    /**
     * auth-client 中的拦截器需要排除拦截的地址
     */
    String[] urls = {
            "/error",
            "/login",
            "/v2/api-docs",
            "/v2/api-docs-ext",
            "/swagger-resources/**",
            "/webjars/**",
            "/",
            "/csrf",
            "/META-INF/resources/**",
            "/resources/**",
            "/static/**",
            "/public/**",
            "classpath:/META-INF/resources/**",
            "classpath:/resources/**",
            "classpath:/static/**",
            "classpath:/public/**",
            "/cache/**",
            "/swagger-ui.html**",
            "/doc.html**"
    };

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor())
                .addPathPatterns("/**");

        // registry.addInterceptor(new MyInterceptor())
        //         .addPathPatterns("/**")
        //         .excludePathPatterns(urls);
        super.addInterceptors(registry);
    }

    /**
     * 自定义静态资源映射目录
     * 添加静态资源文件, 外部可以直接访问地址, 且路径下的所有文件均可被用户直接读取
     * #
     * 由于继承了WebMvcConfigurationSupport
     * 则在yml中配置的mvc相关配置会失效, 需要重新指定静态资源
     * #
     * 如果使用WebMvcConfigurer, 这个资源路径不需要指定
     *
     * @param registry
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        // LIST.forEach(item -> registry.addResourceHandler(item));
        /**
         * springboot默认的静态资源路径匹配的正则表达式为 "/**"
         * 它自动匹配到  /static, /public, /resources, /META-INF/resources 四个目录
         * 且是个目录的顺序是    META-INF/resources > resources > static > public
         * #
         * 需求: 将用户的上传图片(静态文件)的路径映射到本地磁盘的路径 -> 需要自定义静态资源路径
         * #
         * 这样的写法会使得 springboot中默认的路径给覆盖掉了, 默认路径都失效了
         * registry.addResourceHandler("/**").addResourceLocations("file:D:/image-tmp/");
         * #
         * 解决方式
         * 方式一: 在配置类中将默认的正则表达式对应的地址都加上
         * 方式二: 在application.yml配置文件中, 修改spring.resources.static-locations属性, 追加额外的路径(记得带上原来的四个路径)
         */
        registry.addResourceHandler("/**")
                .addResourceLocations("file:D:/image-tmp/")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/resources")
                .addResourceLocations("classpath:/public")
                .addResourceLocations("classpath:/META-INF/resources");
        // swagger的静态资源路径前缀是 /META-INF/resources
        // addResourceHandler:  指的是对外暴露的访问地址
        // addResourceLocations:指的是内部文件放置的目录
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }

}
